require "test/unit"
class AboutExceptions<Test::Unit::TestCase
  class MySpecialError<RuntimeError
  end

  def test_string1
    assert_equal RuntimeError,    MySpecialError.ancestors[1]
    assert_equal StandardError,   MySpecialError.ancestors[2]
    assert_equal Exception,       MySpecialError.ancestors[3]
    assert_equal Object,          MySpecialError.ancestors[4]
  end
  # 第一个是寻找MySpecialError的父类，即RuntimeError,后面找RuntimeError的父类，以此类推，RuntimeError父类为Excoption, Exception父类为Object

  def test_string2
    result = nil
    begin
      fail "Oops"
    rescue StandardError => ex
      result = :exception_handled
    end

    assert_equal :exception_handled, result
    assert_equal true,  ex.is_a?(StandardError), "Should be a Standard Error"
    assert_equal true,  ex.is_a?(RuntimeError),  "Should be a Runtime Error"
    
    assert RuntimeError.ancestors.include?(StandardError),
      "RuntimeError is a subclass of StandardError"
    assert_equal "Oops", ex.message
  end
 #说明RuntimeError是StandardError的一个子类，ex.message显示Standard的信息

  def test_string3
    result = nil
    begin
      # 'raise' and 'fail' are synonyms
      raise MySpecialError, "My Message"
    rescue MySpecialError => ex
      result = :exception_handled
    end
    assert_equal :exception_handled, result
    assert_equal "My Message", ex.message
  end
  # raise 和fail同义，   其他的同上

  def test_string4
    result = nil
    begin
      fail "Oops"
    rescue StandardError => ex
      # no code here
    ensure
      result = :always_run
    end
  
    assert_equal :always_run,  result
  end
  # ensure   确保  result = :always_run ,

  def test_string5
    # A do-end is a block, a topic to explore more later
    assert_raise (AboutExceptions::MySpecialError) do
      raise MySpecialError.new("New instances can be raised directly.")
    end
  end
 # 这个位置目前还不懂，查好之后补充
end
